package elitech.vietnam.copyweatherfashion;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.ProgressDialogFragment;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

public class SignupActivity extends FragmentActivity {

	public static final String	EXTRAS_SIGNUPUSERNAME	= "EXTRAS_SIGNUPUSERNAME";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

		SignupActivity	mActivity;
		TextView		mTxtError;
		EditText		mEdtUsername, mEdtPassword, mEdtPassword1;
		Button			mBtnSubmit;

		public PlaceholderFragment() {
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			mActivity = (SignupActivity) getActivity();
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.fragment_signup, container, false);

			mTxtError = (TextView) view.findViewById(R.id.signup_txtError);
			mBtnSubmit = (Button) view.findViewById(R.id.signup_btnSubmit);
			mEdtPassword = (EditText) view.findViewById(R.id.signup_edtPassword);
			mEdtPassword1 = (EditText) view.findViewById(R.id.signup_edtPassword1);
			mEdtUsername = (EditText) view.findViewById(R.id.signup_edtUsername);

			mBtnSubmit.setOnClickListener(this);

			return view;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.signup_btnSubmit:
				if (mEdtUsername.getText().toString().length() == 0)
					mTxtError.setText(R.string.please_enter_username);
				else if (mEdtPassword.getText().toString().length() == 0)
					mTxtError.setText(R.string.please_enter_password);
				else if (mEdtPassword1.getText().toString().length() == 0)
					mTxtError.setText(R.string.please_confirm_password);
				else if (!mEdtPassword1.getText().toString().equals(mEdtPassword.getText().toString()))
					mTxtError.setText(R.string.the_password_confirmed_did_not_match_);
				else {
					mTxtError.setText("");
					new SignupTask().execute();
				}
				break;

			default:
				break;
			}
		}

		class SignupTask extends AsyncTask<Integer, Integer, Integer> {
			ProgressDialogFragment	mProgress	= new ProgressDialogFragment.Builder()
														.setTitle(getString(R.string.loading))
														.setMessage(getString(R.string.please_wait)).build();

			@Override
			protected void onPreExecute() {
				mProgress.show(getFragmentManager());
			}

			@Override
			protected Integer doInBackground(Integer... params) {
				try {
					return new Service().signUp(mEdtUsername.getText().toString(), mEdtPassword.getText().toString(),
							mEdtUsername.getText().toString(), "", "", Const.COUNTRYCODE);
				} catch (IOException | XmlPullParserException e) {
					e.printStackTrace();
					return -3;
				}
			}

			@Override
			protected void onPostExecute(Integer result) {
				mProgress.dismiss();
				if (result == -1)
					mTxtError.setText(R.string.register_user_exist);
				else if (result == -3)
					mTxtError.setText(R.string.cannot_connect_to_server);
				else {
					Toast.makeText(mActivity, R.string.registersuccessful, Toast.LENGTH_SHORT).show();
					Intent intent = new Intent();
					intent.putExtra(EXTRAS_SIGNUPUSERNAME, mEdtUsername.getText().toString());
					mActivity.setResult(RESULT_OK, intent);
					mActivity.finish();
				}
			}
		}
	}

}
